package com.graduation.alien_yuan.smartmusicplayer.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.graduation.alien_yuan.smartmusicplayer.R;
import com.graduation.alien_yuan.smartmusicplayer.bean.MusicModel;
import com.graduation.alien_yuan.smartmusicplayer.utils.LogUtils;

import java.util.List;

/**
 * Created by Alien_yuan on 2016/3/6.
 */
public class LocalMusicListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<MusicModel> mDataList;
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public LocalMusicListAdapter(Context mContext, List<MusicModel> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_music_card,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder)holder;
        itemViewHolder.cover.setImageURI(mDataList.get(position).getCoverUri());
        LogUtils.d("alien-image uri:"+mDataList.get(position).getCoverUri());
        itemViewHolder.title.setText(mDataList.get(position).getTitle());
        itemViewHolder.artist.setText(mDataList.get(position).getArtist());
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public MusicModel getItemObject(int position){
        return mDataList.get(position);
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        boolean onItemLongClick(View view,int position);
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{

        public ImageView cover;
        public TextView title;
        public TextView artist;

        public ItemViewHolder(View itemView) {
            super(itemView);
            cover = (ImageView)itemView.findViewById(R.id.item_music_card_cover);
            title = (TextView)itemView.findViewById(R.id.item_music_card_title);
            artist = (TextView)itemView.findViewById(R.id.item_music_card_artist);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (onItemClickListener != null){
                return onItemClickListener.onItemLongClick(v,getAdapterPosition());
            }else {
                return false;
            }
        }
    }
}
